package com.dlmz.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;
import com.dlmz.util.ResultSetToUser;

@SuppressWarnings("serial")
public class LoginJframe extends JFrame{
	
	JLabel jl1,jl2,jl4,jl5;       //jl1 工号标签，jl2密码标签，jl4提示注册，jl5注册
	JTextField jtfField;                //工号输入框
	JPasswordField jtJPasswordField;    //密码输入框
	JButton jb,jb2;                     //jb登录， jb2重置
	
	public LoginJframe() {
		init();                        //初始化
		listener();                    //设置监听器
	}
	
	private void listener() {
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				String username=jtfField.getText().trim();     //trim( )的作用是去掉字符串两端的多余的空格
				String pwd=new String(jtJPasswordField.getPassword()).trim();
				//创建查询的sql语句
				String sql="select * from user where Account=? and Passwd=?";
				//创建BaseDao
				BaseDao baseDao=new BaseDao(); 
				List<Object> list=baseDao.query(sql,new ResultSetToUser(),username,pwd); //给查询传参，同时调用ResultSetToUser
				if(list.size()>0) {
					JOptionPane.showMessageDialog(null, "登陆成功"); 
					dispose();           //只保留下面的窗口
					new MenuJframe();      //菜单页面
				}else {
					JOptionPane.showMessageDialog(null, "登陆失败"); }	 
			}
		});
		jb2.addActionListener(new ActionListener() { //重置按钮
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jtfField.setText("");
				jtJPasswordField.setText("");
			}
		});
		jl5.addMouseListener(new MouseListener() {   //注册
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {   //不碰到该组件默认黑色
				// TODO Auto-generated method stub
				jl5.setForeground(Color.black);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) { //碰到组件变成蓝色
				// TODO Auto-generated method stub
				jl5.setForeground(Color.blue);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {  //点击组件进入注册界面
				// TODO Auto-generated method stub
				dispose();
				new RegistJframe();
			}
		});
	}
	
	public void init() {            //一些初始化位置，字体等方面的配置
		jl1=new JLabel("工号:");
		jl2=new JLabel("密 码:");
		jl4=new JLabel("没有账号？");
		jl5=new JLabel("点击注册");
		//设置jl1的位置
		jl1.setBounds(100,80, 80, 30);
		//设置jl2的位置
		jl2.setBounds(100,140, 100, 30);
		jl4.setBounds(150,270,80,30);
		jl5.setBounds(220,270,60,30);
		//设置jl1的字体
		jl1.setFont(new Font("楷体", Font.BOLD, 20));
		//设置jl2的字体
		jl2.setFont(new Font("楷体", Font.BOLD, 20));
			
		jtfField=new JTextField();
		jtfField.setBounds(180,80,130,30);
		jtJPasswordField=new JPasswordField();
		jtJPasswordField.setBounds(180,140,130,30);
		jb=new JButton("登录");
		jb2=new JButton("重置");
		jb.setBounds(130, 220, 60, 30);
		jb2.setBounds(230, 220, 60, 30);
		this.setLayout(null);        //空布局以保证组件能够按固定位置去放置
		//将组件添加到窗口中
		this.add(jl1);
		this.add(jtfField);
		this.add(jl2); 
		this.add(jtJPasswordField);
		this.add(jb);
		this.add(jb2);
		this.add(jl4);
		this.add(jl5);
		this.setTitle("登陆界面");
		this.setSize(400,370);
		//设置窗口居中
		this.setLocationRelativeTo(null);
		//设置窗口是否可见
		this.setVisible(true);
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	public static void main(String[] args) {
		new LoginJframe();
	}
}
